using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The PaymentTypeCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about PaymentTypeCodeTable objects.
/// </summary>
public class PaymentTypeCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of PaymentTypeCodeTable from the underlying datasource.
/// </summary>
/// <param name="paymentTypeCode">The unique PaymentTypeCode of the PaymentTypeCodeTable in the database.</param>
/// <returns>An PaymentTypeCodeTable when the PaymentTypeCode was found in the database, or null otherwise.</returns>
public static PaymentTypeCodeTable GetItem(Int16 paymentTypeCode)
{
PaymentTypeCodeTable myPaymentTypeCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPaymentTypeCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@paymentTypeCode", paymentTypeCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myPaymentTypeCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myPaymentTypeCodeTable;
}
}

/// <summary>
/// Returns a list with PaymentTypeCodeTable objects.
/// </summary>
/// <returns>A generics List with the PaymentTypeCodeTable objects.</returns>
public static PaymentTypeCodeTableList GetList()
{
PaymentTypeCodeTableList tempList = new PaymentTypeCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPaymentTypeCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a PaymentTypeCodeTable in the database.
/// </summary>
/// <param name="myPaymentTypeCodeTable">The PaymentTypeCodeTable instance to save.</param>
/// <returns>The new PaymentTypeCode if the PaymentTypeCodeTable is new in the database or the existing PaymentTypeCode when an item was updated.</returns>
public static Int16 Save(PaymentTypeCodeTable myPaymentTypeCodeTable)
{
   Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPaymentTypeCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myPaymentTypeCodeTable.PaymentTypeCode == -1)
{myCommand.Parameters.AddWithValue("@paymentTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@paymentTypeCode", myPaymentTypeCodeTable.PaymentTypeCode);
}

if (myPaymentTypeCodeTable.CategoryType == null)
{
    myCommand.Parameters.AddWithValue("@CategoryType", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@CategoryType", myPaymentTypeCodeTable.CategoryType);
}

if (myPaymentTypeCodeTable.Description == null){
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myPaymentTypeCodeTable.Description);
}
if (myPaymentTypeCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myPaymentTypeCodeTable.Status);
}
if (myPaymentTypeCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myPaymentTypeCodeTable.CreatedDateTime);
}
if (myPaymentTypeCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myPaymentTypeCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a PaymentTypeCodeTable from the database.
/// </summary>
/// <param name="paymentTypeCode">The PaymentTypeCode of the PaymentTypeCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int16 paymentTypeCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPaymentTypeCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@paymentTypeCode", paymentTypeCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the PaymentTypeCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static PaymentTypeCodeTable FillDataRecord(IDataRecord myDataRecord )
{
PaymentTypeCodeTable myPaymentTypeCodeTable = new PaymentTypeCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentTypeCode")))
{
myPaymentTypeCodeTable.PaymentTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CategoryType")))
{
    myPaymentTypeCodeTable.CategoryType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CategoryType"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myPaymentTypeCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myPaymentTypeCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myPaymentTypeCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myPaymentTypeCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myPaymentTypeCodeTable;
}
}
